/*
 * @Discription: 
 * @Company: 512354087@qq.com
 * @Author: Donie
 * @LastEditors: OBKoro1
 * @Date: 2019-05-10 15:03:28
 * @LastEditTime: 2019-05-10 15:08:59
 */
/** 百度API封装 **/
function bdPromisify(fn) {
	return function (obj = {}) {
		return new Promise((resolve, reject) => {
			obj.success = function (res) {
				//成功
				resolve(res)
			}
			obj.fail = function (res) {
				//失败
				reject(res)
			}
			fn(obj)
		})
	}
}
//无论promise对象最后状态如何都会执行
Promise.prototype.finally = function (callback) {
	let P = this.constructor;
	return this.then(
		value => P.resolve(callback()).then(() => value),
		reason => P.resolve(callback()).then(() => { throw reason })
	);
};
/**
 * 百度用户登录,获取code
 */
function bdLogin() {
	return bdLogin(swan.login)
}
/**
 * 获取百度用户信息
 * 注意:须在登录之后调用
 */
function GetUserInfo() {
	return bdLogin(swan.getUserInfo)
}
/**
 * 获取系统信息
 */
function bdGetSystemInfo() {
	return bdLogin(swan.getSystemInfo)
}
/**
 * 保留当前页面，跳转到应用内的某个页面
 * url:'../index/index'
 * params:{key:value1}
 */
function bdNavigateTo(url, params) {
	var bdNavigateTo = bdLogin(swan.navigateTo)
	const serializedParams = this.paramSerializer(params)
	if (serializedParams.length > 0) {
		url += ((url.indexOf('?') == -1) ? '?' : '&') + serializedParams
	}
	return bdNavigateTo({
		url: url
	})	
}
module.exports = {
	bdLogin: bdLogin,
	bdLogin: bdLogin,
	GetUserInfo: GetUserInfo,
	bdGetSystemInfo: bdGetSystemInfo,
}